package it.polimi.it.TravelDream.entities;

import java.util.ArrayList;
import java.util.List;

import it.polimi.it.TravelDream.entities.Hotel;
import it.polimi.it.TravelDream.ejb.eaos.ExcursionDTO;
import it.polimi.it.TravelDream.ejb.eaos.FlightDTO;
import it.polimi.it.TravelDream.ejb.eaos.HotelDTO;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class BasicProdMgrBean
 */
@Stateless
@LocalBean
public class BasicProdMgrBean implements BasicProdMgr {

	@PersistenceContext
    private EntityManager em;
	
	public void purchase(HotelDTO hotel){
		String emailBuyer = hotel.getIdbuyer();
		Purchas purchase= new Purchas(hotel,emailBuyer);
		em.persist(purchase);
		remove(hotel);
	}
	
	public void purchase(FlightDTO flight){
		String emailBuyer = flight.getIdBuyer();
		Purchas purchase= new Purchas(flight,emailBuyer);
		em.persist(purchase);
		remove(flight);
	}
	
	public void purchase(ExcursionDTO excursion){
		String emailBuyer = excursion.getIdbuyer();
		Purchas purchase= new Purchas(excursion,emailBuyer);
		em.persist(purchase);
		remove(excursion);
	}
	
	@Override
	public ArrayList<HotelDTO> getHotelsList(String keyword){
		return listHotelConverterTDO( em.createNamedQuery(Hotel.FIND_ALL, Hotel.class)
                .getResultList());
	}
	
	@SuppressWarnings("unchecked")
	@Override
	 public ArrayList<HotelDTO> getHotelDetails(String idHotel){
		return listHotelConverterTDO(em.createQuery(
					"SELECT h FROM Hotel h WHERE h.idbasicproduct LIKE :idHotel").setParameter("idHotel", idHotel).getResultList());
	 }
	@SuppressWarnings("unchecked")
	@Override
	 public ArrayList<FlightDTO> getFlightDetails(String idFlight){
		return listFlightConverterTDO(em.createQuery(
					"SELECT f FROM Flight f WHERE f.idbasicproduct LIKE :idFlight").setParameter("idFlight", idFlight).getResultList());
	 }
	@SuppressWarnings("unchecked")
	@Override
	 public ArrayList<ExcursionDTO> getExcursionDetails(String idExcursion){
		return listExcursionConverterTDO(em.createQuery(
					"SELECT e FROM Excursion e WHERE e.idbasicproduct LIKE :idExcursion").setParameter("idExcursion", idExcursion).getResultList());
	 }
	 
	 
	 
	 @Override
	public ArrayList<FlightDTO> getFlightsList(){
		List<Flight> list;
		list =( em.createNamedQuery(Flight.FIND_ALL, Flight.class)
                .getResultList());
		 System.out.println(list.get(0).getArrivalairport());
		return listFlightConverterTDO( em.createNamedQuery(Flight.FIND_ALL, Flight.class)
                .getResultList());
	}
	
	@Override
	public ArrayList<ExcursionDTO> getExcursionsList(){
		List<Excursion> list;
		list =( em.createNamedQuery(Excursion.FIND_ALL, Excursion.class)
                .getResultList());
		 System.out.println(list.get(0).getExcursiontype());
		return listExcursionConverterTDO( em.createNamedQuery(Excursion.FIND_ALL, Excursion.class)
                .getResultList());
	}
	//converte tutta lista di hotel in hotel DTO
	private ArrayList<HotelDTO> listHotelConverterTDO(List<Hotel>hotelsList){
		int i;
		ArrayList<HotelDTO> newList = new ArrayList<HotelDTO>();
		for( i=0;i<hotelsList.size();i++){
			newList.add(convertHotelToDTO(hotelsList.get(i)));
		}
		return newList;
	}
	
	private ArrayList<FlightDTO> listFlightConverterTDO(List<Flight>flightsList){
		int i;
		ArrayList<FlightDTO> newList = new ArrayList<FlightDTO>();
		for( i=0;i<flightsList.size();i++){
			newList.add(convertFlightToDTO(flightsList.get(i)));
		}
		return newList;
	}
	
	private ArrayList<ExcursionDTO> listExcursionConverterTDO(List<Excursion>excursionsList){
		int i;
		ArrayList<ExcursionDTO> newList = new ArrayList<ExcursionDTO>();
		for( i=0;i<excursionsList.size();i++){
			newList.add(convertExcursionToDTO(excursionsList.get(i)));
		}
		return newList;
	}
	//metodo che converte un hotel in un hotelDTO
	private HotelDTO convertHotelToDTO(Hotel hotel) {
		HotelDTO hotelDTO = new HotelDTO();
		hotelDTO.setCity(hotel.getCity());
		hotelDTO.setDatain(hotel.getDatain());
		hotelDTO.setDataout(hotel.getDataout());
		hotelDTO.setIdbasicproduct(hotel.getIdbasicproduct());
		hotelDTO.setIdbuyer(hotel.getIdbuyer());
		hotelDTO.setIdpackage(hotel.getIdpackage());
		hotelDTO.setName(hotel.getName());
		hotelDTO.setPrice(hotel.getPrice());
		hotelDTO.setRoomnumber(hotel.getRoomnumber());
		return hotelDTO;
	}
	
	private ExcursionDTO convertExcursionToDTO(Excursion excursion) {
		ExcursionDTO excursionDTO = new ExcursionDTO();
		 excursionDTO.setExcursiontype(excursion.getExcursiontype());
		 excursionDTO.setIdbasicproduct(excursion.getIdbasicproduct());
		 excursionDTO.setIdbuyer(excursion.getIdbuyer());
		 excursionDTO.setIdpackage(excursion.getIdpackage());
		 excursionDTO.setLocality(excursion.getLocality());
		 excursionDTO.setPrice(excursion.getPrice());
		 excursionDTO.setProvince(excursion.getProvince());
		
		return excursionDTO;
	}
	
	private FlightDTO convertFlightToDTO(Flight flight) {
		FlightDTO flightDTO = new FlightDTO();
		flightDTO.setArrivalairport( flight.getArrivalairport());
		flightDTO.setArrivaldate( flight.getArrivaldate());
		flightDTO.setDepartureairport( flight.getDepartureairport());
		flightDTO.setDeparturedate( flight.getDeparturedate());
		flightDTO.setIdbasicproduct(flight.getIdbasicproduct());
		flightDTO.setIdBuyer( flight.getIdBuyer());
		flightDTO.setPrice(flight.getPrice());
		flightDTO.setSeatnumber( flight.getSeatnumber());
		return flightDTO;
	}
	 public void remove(HotelDTO hotel) {
			Hotel newHotel = findHotel(hotel.getIdbasicproduct());
	        em.remove(newHotel);
		}
	 
	
	 public Hotel findHotel(String idbasicprod) {
	    	return em.find(Hotel.class, idbasicprod);
	    	
	    }
	 public void remove(FlightDTO flight) {
			Flight newFlight = findFlight(flight.getIdbasicproduct());
	        em.remove(newFlight);
		}

	
	 public Flight findFlight(String idbasicprod) {
	    	return em.find(Flight.class, idbasicprod);
	    }
	 
	 public void remove(ExcursionDTO excursion) {
			Excursion newExcursion = findExcursion(excursion.getIdbasicproduct());
	        em.remove(newExcursion);
		}

	
	 public Excursion findExcursion(String idbasicprod) {
	    	return em.find(Excursion.class, idbasicprod);
	    }
	
	
    /**
     * Default constructor. 
     */
    public BasicProdMgrBean() {
        // TODO Auto-generated constructor stub
    }

}
